/*
 * Main.java
 *
 * Created on 18 de julio de 2007, 16:58
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fresita;

import java.util.Date;
import java.util.Vector;
import javax.xml.ws.Endpoint;
import model.*;
/**
 *
 * @author mariano
 */
public class Main {
    
    /** Creates a new instance of Main */
    public Main() {
    }
    
    public static void loop()
    {
        // this is the way to make the local web server running until the process is killed
        while (true) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ex) {}
        }
    }
    
    public static void client()
    {
        Vector<Author> vAuthors = new Vector<Author>();
        vAuthors.add( new Author("Mariano", "Guerra", "luismarianoguerra@gmail.com") );
        vAuthors.add( new Author("Papa", "Noel", "papanoel@gmail.com") );
        
        Vector<String> vTags = new Vector<String>();
        vTags.add( "programacion" );
        vTags.add( "refactoring" );
        vTags.add( "sushi" );
        
        Vector<Revision> vRevisions = new Vector<Revision>();
        vRevisions.add( new Revision(new Date(),vAuthors,"nada en particular", 2 ) );
        vRevisions.add( new Revision(new Date(),vAuthors,"nada...", 3 ) );
        
        Vector<Step> vSteps = new Vector<Step>();
        vSteps.add( new Step( "paso 1", "descripcion 1" ) );
        vSteps.add( new Step( "paso 2", "descripcion 2" ) );
        
        Vector<Ingredient> vIngredients = new Vector<Ingredient>();
        Ingredient ingredient = new Ingredient("como refactorizar java", 1, vAuthors, new Vector<Revision>(), "como hacerlo en java", vTags, vSteps );
        vIngredients.add( ingredient );
        
        Recipe recipe = new Recipe( 3, "How to refactore code", vAuthors, vTags, "otro howto mas", vRevisions, vIngredients );
        
        /*
        System.out.println( recipe.toXmlString() );
        Recipe r = Recipe.fromXml( recipe.toXmlString() );
        System.out.println( r.toXmlString() );
        */
        
        ws.RecipeClient rc = new ws.RecipeClient( "http://localhost:8765/", "Fresitaws", "FresitaPort", "http://localhost:8765/", "http://fresita/" );
        System.out.println((rc.ping())?"pong!":"nothing");
        System.out.println((rc.addRecipe(recipe))?"ok!":"error adding recipe");
        System.out.println((rc.addIngredient(ingredient))?"ok!":"error adding ingredient");
        Recipe rec = rc.getRecipe("How to refactore code.xml");
        if(rec != null)
            System.out.println(rec.toXmlString());
        else
            System.out.println("recipe is null");
        
        Ingredient ing = rc.getIngredient("como refactorizar java.xml");
        if(ing != null)
            System.out.println(ing.toXmlString());
        else
            System.out.println("ingredient is null");
        
    }
    
    public static void server()
    {
        Controller controller = new Controller();
        ws.MachineList machineList = new ws.MachineList();
        
        int port = 8765;
        String host = "http://localhost:" + port + "/";
        Endpoint.publish(host, new ws.RecipeServer(machineList, controller));
        System.out.println("Web service was published successfuly.\nWSDL URL: "+host+"?WSDL");
        
        loop();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        /*
        System.out.println( recipe.toXmlString() );
        Recipe r = Recipe.fromXml( recipe.toXmlString() );
        System.out.println( r.toXmlString() );
        */
        
        try
        {
            // forma turbia de hacer que si el server ya esta corriendo
            // corra el cliente
            server();
        }
        catch(Exception ex)
        {
            System.err.println(ex.toString());
            client();
        }
    }
    
}
